package com.aster.common.api.controller;

import com.aster.common.api.domain.TbGeneralUserInfo;
import com.aster.common.api.dto.DtoGeneralUserInfo;
import com.aster.common.api.dto.dt2dto.GeneralUserInfoConvert;
import com.aster.common.api.service.AppTokenService;
import com.aster.common.api.service.GeneralUserInfoService;
import com.aster.common.api.service.SmsCheckService;
import com.aster.common.base.BaseController;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.R;
import com.aster.common.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * 
 * <br>
 * <b>功能：</b>TbGeneralUserInfoController<br>
 *   <br>
 */ 
@RestController @RequestMapping("/generalUserInfo")
public class GeneralUserInfoController extends BaseController {
	
	private final static Logger log = Logger.getLogger(GeneralUserInfoController.class);
	@Autowired(required=false) 
	private GeneralUserInfoService generalUserInfoService;
	@Autowired
	private AppTokenService appTokenService;
	@Autowired
	private SmsCheckService smsCheckService;

	@RequestMapping("/getUserByOpenId")
	public R getUserByOpenId(String openId){
		if(StringUtils.isEmpty(openId)){
			return R.error(10001,"传入参数为空");
		}
		TbGeneralUserInfo tbGeneralUserInfo = generalUserInfoService.getUserByOpenId(openId);
		if(tbGeneralUserInfo == null){
			return R.error(20001,"查询不到该用户");
		}
		DtoGeneralUserInfo dtoGeneralUserInfo = GeneralUserInfoConvert.MAPPER.do2dto(tbGeneralUserInfo);
		try {
			String token = appTokenService.updateToken(openId);
			dtoGeneralUserInfo.setToken(token);
		}catch (Exception ex){
			log.error(ex.getMessage());
			return R.error(55555,ex.getMessage());
		}
		return R.ok().put("data",dtoGeneralUserInfo);
	}

	@RequestMapping("/getUserByToken")
	public R getUserByToken(String token){
		if(StringUtils.isEmpty(token)){
			return R.error(10001,"传入参数为空");
		}
		try {
			String openId = appTokenService.getOpenIdByToken(token);
			if(StringUtils.isEmpty(openId)){
				return R.error(20002,"token过期");
			}
			TbGeneralUserInfo tbGeneralUserInfo = generalUserInfoService.getUserByOpenId(openId);
			if(tbGeneralUserInfo == null){
				return R.error(20001,"查询不到该用户");
			}
			DtoGeneralUserInfo dtoGeneralUserInfo = GeneralUserInfoConvert.MAPPER.do2dto(tbGeneralUserInfo);
			return R.ok().put("data",dtoGeneralUserInfo);
		}catch (Exception ex){
			log.error(ex.getMessage());
			return R.error(55555,ex.getMessage());
		}
	}

	@RequestMapping("/bindingUser")
	R bindingUser(String openId,String tel,String code){
		if(StringUtils.isEmpty(openId) || StringUtils.isEmpty(tel) || StringUtils.isEmpty(code)){
			return R.error(55555,"参数异常");
		}
		try {
			TbGeneralUserInfo tbGeneralUserInfo = generalUserInfoService.getUserByOpenId(openId);
			if(tbGeneralUserInfo != null){
				String token = appTokenService.updateToken(openId);
				DtoGeneralUserInfo dtoGeneralUserInfo = GeneralUserInfoConvert.MAPPER.do2dto(tbGeneralUserInfo);
				dtoGeneralUserInfo.setToken(token);
				return R.ok().put("data",dtoGeneralUserInfo);
			}
			smsCheckService.generalUserLoginCodeCheck(tel,code);
			String token = appTokenService.updateToken(openId);
			tbGeneralUserInfo = new TbGeneralUserInfo();
			tbGeneralUserInfo.setTel(tel);
			tbGeneralUserInfo.setOpenId(openId);
			tbGeneralUserInfo.setUnionId(openId);
			generalUserInfoService.saveUserInfo(tbGeneralUserInfo);
			DtoGeneralUserInfo dtoGeneralUserInfo = GeneralUserInfoConvert.MAPPER.do2dto(tbGeneralUserInfo);
			dtoGeneralUserInfo.setToken(token);
			return R.ok().put("data",dtoGeneralUserInfo);
		}catch (ServiceException ex) {
			log.error(ex.getMessage());
			return R.error(55555,ex.getMessage());
		}
	}
}
